package com.mlm.mlmapp.adapter.view.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;

import com.androidquery.AQuery;
import com.mlm.mlmapp.R;
import com.mlm.mlmapp.activity.HairDresserListActivity_;
import com.mlm.mlmapp.adapter.view.listener.OnSyClickedListener;
import com.mlm.mlmapp.cons.HTTPCons;

import org.json.JSONArray;
import org.json.JSONException;

public class MyPagerAdapter extends PagerAdapter implements HTTPCons {
    AQuery aq;
    Context con;
    ImageView image;
    ImageView image1;
    private JSONArray array;
    private String actID;
    private JSONArray contents;
    private OnSyClickedListener onSyListener;
    private Bitmap prebitmap;

    public MyPagerAdapter(Context con, JSONArray array, OnSyClickedListener onSyListener) {
        // this.viewList=viewList;
        this.array = array;
        this.con = con;
        aq = new AQuery(con);
        this.onSyListener = onSyListener;
        prebitmap= BitmapFactory.decodeResource(con.getResources(),R.drawable.homelist_default);
    }

    @SuppressLint("NewApi")
    @Override
    public Object instantiateItem(final ViewGroup container,
                                  final int position) {

        if (position == array.length()) {
            LayoutInflater inflater = LayoutInflater.from(con);
            View r = inflater.inflate(R.layout.more_dresser, container, false);
            r.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    HairDresserListActivity_.intent(con).extra("bookflag",false).flags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT).start();
                }
            });
            container.removeView(r);
            container.addView(r);
            return r;
        }
        image = new ImageView(con);
        image.setScaleType(ImageView.ScaleType.FIT_XY);
        image.setLayoutParams(new LayoutParams(200, -1));
        image.setMaxWidth(image.getWidth());
        image.setMaxHeight(image.getHeight());
//			image=(ImageView)inf.inflate(R.layout.include_view_pager, null).findViewById(R.id.iv_sylist);
//			image.setAdjustViewBounds(true);
////			image.setScaleType(ScaleType.CENTER_CROP);
        try {
            image.setTag(array.getJSONObject(position).get(ID_SYLIST)
                    .toString());
            aq.id(image).image(array.getJSONObject(position).get(IMG_SYLIST)
                            .toString(),
                    true,
                    true,
                    400,
                    0,prebitmap
                    ,
                    AQuery.FADE_IN);
                image.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View arg0) {
                        // TODO Auto-generated method stub
                        try {
                            onSyListener.onSyItemClick(arg0, array.getJSONObject(position).get(ID_SYLIST)
                                    .toString());
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
//            aq.ajax(array.getJSONObject(position).get(IMG_SYLIST)
//                    .toString(), Bitmap.class, new AjaxCallback<Bitmap>() {
//
//                @Override
//                public void callback(String url, Bitmap object,
//                                     AjaxStatus status) {
//                    if (status.getCode() == -101) {
//                        Toast.makeText(aq.getContext(), "网络异常,请检查网络", Toast.LENGTH_SHORT);
//                        return;
//                    }
//                    try {
//                        final String id = array
//                                .getJSONObject(position).get(ID_SYLIST)
//                                .toString();
//                        image = (ImageView) container.findViewWithTag(id);
//                        image.setImageBitmap(object);
//                        image.setOnClickListener(new OnClickListener() {
//                            @Override
//                            public void onClick(View arg0) {
//                                // TODO Auto-generated method stub
//                                onSyListener.onSyItemClick(arg0, id);
//                            }
//                        });
//                    } catch (JSONException e) {
//                        // TODO Auto-generated catch block
//                        e.printStackTrace();
//                    }
//
//                    container.removeView(image);
//                    container.addView(image);
//
//                    // view.setBackgroundColor(Color.argb(255, 1 *
//                    // 50,
//                    // 1 * 10, 1 * 50));
//                }
//            });
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        container.removeView(image);
        container.addView(image);
        this.actID = image.getTag().toString();
        return image;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public int getCount() {
        return array.length() + 1;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return (view == object);
    }

    //
    @Override
    public float getPageWidth(int position) {
        // TODO Auto-generated method stub
        return 1f;
    }

    public String getActID(int position) {
        String str = "";
        try {
            str = array.getJSONObject(position).get(ID_SYLIST)
                    .toString();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return str;
    }

}